package com.hwadee.property.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hwadee.property.pojo.UserActivityLog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
@Mapper
public interface UserAcitivityLogMapper extends BaseMapper<UserActivityLog> {

    @Select("<script>" +
            "SELECT * FROM user_activity_log " +
            "<where>" +
            "   <if test='userId != null'>AND user_id = #{userId} </if>" +
            "   <if test='communityId != null'>AND community_id = #{communityId} </if>" +
            "   <if test='userType != null'>AND user_type = #{userType} </if>" +
            "   <if test='logType != null'>AND log_type = #{logType} </if>" +
            "   <if test='startTime != null'>AND activity_time &gt;= #{startTime} </if>" +
            "   <if test='endTime != null'>AND activity_time &lt;= #{endTime} </if>" +
            "</where>" +
            "ORDER BY activity_time ASC " +
            "LIMIT #{offset}, #{pageSize}" +
            "</script>")
    List<UserActivityLog> getActivityLogsByPage(@Param("userId") Long userId,
                                                @Param("communityId") Long communityId,
                                                @Param("userType") Integer userType,
                                                @Param("logType") String logType,
                                                @Param("startTime") String startTime,
                                                @Param("endTime") String endTime,
                                                @Param("offset") int offset,
                                                @Param("pageSize") int pageSize);

    @Select("<script>" +
            "SELECT COUNT(*) FROM user_activity_log " +
            "<where>" +
            "   <if test='userId != null'>AND user_id = #{userId} </if>" +
            "   <if test='communityId != null'>AND community_id = #{communityId} </if>" +
            "   <if test='userType != null'>AND user_type = #{userType} </if>" +
            "   <if test='logType != null'>AND log_type = #{logType} </if>" +
            "   <if test='startTime != null'>AND activity_time &gt;= #{startTime} </if>" +
            "   <if test='endTime != null'>AND activity_time &lt;= #{endTime} </if>" +
            "</where>" +
            "</script>")
    int countActivityLogs(@Param("userId") Long userId,
                          @Param("communityId") Long communityId,
                          @Param("userType") Integer userType,
                          @Param("logType") String logType,
                          @Param("startTime") String startTime,
                          @Param("endTime") String endTime);
}
